/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.popup;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.sencha.gxt.widget.core.client.box.MessageBox;


/**
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class MyMessageBox extends MessageBox
{

	private int __height = -1;

	@Override
	public void setPixelSize(final int width, final int height)
	{
		if (this.__height != height + 6)
		{
			this.__height = this.__height < 0 ? height < 0 ? height : height + 6 : height;
		}
		super.setPixelSize(width, this.__height);
	}

	/**
	 * Enumeration for typing message box (change the icon and style)
	 * 
	 * @author tcaselli
	 */
	public static enum MessageType
	{
		/**
		 * For an Info Message box (a blue and nice icon)
		 */
		INFO,
		/**
		 * For a Warning Message box (yellow exclamation mark icon)
		 */
		WARNING,
		/**
		 * For a Question Message box (Blue question mark)
		 */
		QUESTION,
		/**
		 * For and Error Message box (Red cross icon)
		 */
		ERROR
	}

	protected MyMessageBox()
	{
		this((String) null, (String) null, null);
	}

	protected MyMessageBox(final MessageType messageType)
	{
		this((String) null, (String) null, messageType);
	}

	protected MyMessageBox(final String title, final String message, final MessageType messageType)
	{
		super(title, message);
		setMessageType(messageType);
	}

	protected MyMessageBox(final SafeHtml title, final SafeHtml message, final MessageType messageType)
	{
		super(title, message);
		setMessageType(messageType);
	}

	private ImageResource getIcon(final MessageType messageType)
	{
		ImageResource ret = null;
		switch (messageType)
		{
			case INFO:
				ret = MessageBox.ICONS.info();
				break;
			case WARNING:
				ret = MessageBox.ICONS.warning();
				break;
			case QUESTION:
				ret = MessageBox.ICONS.question();
				break;
			case ERROR:
				ret = MessageBox.ICONS.error();
				break;
			default:
				ret = MessageBox.ICONS.info();
				break;
		}
		return ret;
	}

	/**
	 * Sets the message type
	 * 
	 * @param messageType
	 *           the message type
	 */
	public void setMessageType(final MessageType messageType)
	{
		if (messageType != null)
		{
			setIcon(getIcon(messageType));
		}
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// STATIC METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * A Listener indicating what to do after Message box was closed
	 * 
	 * @author tcaselli
	 */
	public interface AfterCloseListener
	{
		/**
		 * Callback method for doing things after the Dialog box was closed
		 */
		public void afterClose();
	}

	/**
	 * Display an Alert Message box with a OK button and the given title and message
	 * 
	 * @param title
	 *           the title
	 * @param message
	 *           the message
	 */
	public static void alert(final String title, final String message)
	{
		alert(title, message, null);
	}

	/**
	 * Display an Alert Message box with a OK button, the given title and message, and the given after close listener
	 * 
	 * @param title
	 *           the title
	 * @param message
	 *           the message
	 * @param afterCloseListener
	 *           the after close listener
	 */
	public static void alert(final String title, final String message, final AfterCloseListener afterCloseListener)
	{
		showMessageBox(title, message, MessageType.ERROR, afterCloseListener);
	}

	/**
	 * Display an Info Message box with a OK button and the given title and message
	 * 
	 * @param title
	 * @param message
	 */
	public static void info(final String title, final String message)
	{
		info(title, message, null);
	}

	/**
	 * Display an Info Message box with a OK button, the given title and message, and the given after close listener
	 * 
	 * @param title
	 *           the title
	 * @param message
	 *           the message
	 * @param afterCloseListener
	 *           the after close listener
	 */
	public static void info(final String title, final String message, final AfterCloseListener afterCloseListener)
	{
		showMessageBox(title, message, MessageType.INFO, afterCloseListener);
	}

	/**
	 * Display a Warning Message box with a OK button and the given title and message
	 * 
	 * @param title
	 * @param message
	 */
	public static void warn(final String title, final String message)
	{
		warn(title, message, null);
	}

	/**
	 * Display a Warning Message box with a OK button, the given title and message, and the given after close listener
	 * 
	 * @param title
	 *           the title
	 * @param message
	 *           the message
	 * @param afterCloseListener
	 *           the after close listener
	 */
	public static void warn(final String title, final String message, final AfterCloseListener afterCloseListener)
	{
		showMessageBox(title, message, MessageType.WARNING, afterCloseListener);
	}

	private static void showMessageBox(final String title, final String message, final MessageType messageType,
			final AfterCloseListener afterCloseListener)
	{
		new MyOkMessageBox(title, message, messageType)
		{
			@Override
			public void processOk()
			{
				if (afterCloseListener != null)
				{
					afterCloseListener.afterClose();
				}
			}

			@Override
			public void processClose()
			{
				if (afterCloseListener != null)
				{
					afterCloseListener.afterClose();
				}
			}
		}.show();
	}

}
